﻿#include"Snake.h"
#include<Windows.h>
#include<stdio.h>
#include<locale.h>
#include<stdbool.h>

#define WALL L'■'
#define HEAD L'○'
#define BODY L'●'
#define FOOD L'★'
#define ORIGIN_X 24
#define ORIGIN_Y 5
#define Key_Press(vk)  ((GetAsyncKeyState(vk)&0x1)?1:0)//查看按键情况，按过改键，会返还一个最低位为1的short类型
//设置光标的坐标
void SetPos(short x, short y)
{
	COORD pos = { x,y };
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//设置标准输出上光标的位置为pos
	SetConsoleCursorPosition(hOutput, pos);
}

void WelcomeToGame()
{
	SetPos(50, 13);
	printf("欢迎来到贪吃蛇小游戏!\n");
	SetPos(50, 25);
	system("pause");
	system("cls");
	SetPos(40, 13);
	printf("请使用↑,↓,←,→分别控制蛇的移动\n");
	SetPos(40, 14);
	printf("按F3加速，F4减速，速度越快得分越高\n");
	SetPos(50, 25);
	system("pause");
	system("cls");
}

void InitMap()
{
	//打印上边界
	SetPos(0, 0);
	for (int i = 0; i <= 56; i += 2)
		wprintf(L"%lc", WALL);
	//下
	SetPos(0, 26);
	for (int i = 0; i <= 56; i += 2)
		wprintf(L"%lc", WALL);
	//左
	for (int i = 1; i <= 25; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	for (int i = 1; i <= 25; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
}

void InitSnake(PSnake snake)
{
	//[2,54]
	//[1,25]
	SetPos(24, 13);
	for (int i = 0; i < 5; i++)
	{
		pNode cur = (pNode)malloc(sizeof(Node));
		cur->_x = ORIGIN_X + 2 * i;
		cur->_y = ORIGIN_Y;
		if (i <= 4)
			wprintf(L"%lc", BODY);
		else
			wprintf(L"%lc", HEAD);
		//头插
		cur->_next = snake->_pSnake;
		snake->_pSnake = cur;
	}
	snake->_Add = 10;
	snake->_status = NORMAL;
	snake->_SleepTime = 200;//200ms
	snake->_dir = RIGHT;
}

bool Against(int x, int y, PSnake snake)
{
	pNode cur = snake->_pSnake;
	while (cur)
	{
		if (cur->_x == x && cur->_y == y)
			return false;
		cur = cur->_next;
	}
	return true;
}

void CreateFood(PSnake snake)
{
	int x, y;
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x % 2 != 0 || !Against(x, y, snake));
	SetPos(x, y);
	pNode cur = (pNode)malloc(sizeof(Node));
	snake->_food = cur;
	wprintf(L"%lc", FOOD);
}

void PrintSocreInfo(PSnake snake)
{
	SetPos(62, 11);
	printf("得分：%d，当前食物可获取得分：%2d", snake->_Score, snake->_Add);
}

void PrintHelpInfo(PSnake snake)
{
	char* fail_condition = "失败条件：撞墙或者咬到自己";
	char* move[] = { "w（向上移动）",
	 "s（向下移动）",
	 "a（向左移动）",
	 "d（向右移动）" ,
	"F3（加速）",
	"F4（减速）" };
	for (int i = 0; i < 4; i++)
	{
		SetPos(62, 15 + i);
		printf("%s", move[i]);
	}
}

void GameStart(PSnake snake)
{
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");
	//获取控制台的句柄，这样就可以操控控制台
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;//结构体，里面储存控制台相关信息
	//获取控制台光标信息
	GetConsoleCursorInfo(hOutput, &CursorInfo);
	//bVisible表示光标填充字符单元格从全填充单元格到单元底部的水平线,1-100
	//CursorInfo.bVisible = 100;
	//dwiSize表示是否显示光标,false是不显示,true是显示
	CursorInfo.dwSize = false;
	//打印欢迎界面
	WelcomeToGame();
	//打印地图
	InitMap();
	//创建食物
	CreateFood(snake);
	//初始化蛇
	InitSnake(snake);
	//打印帮助信息
	PrintHelpInfo(snake);
}

void Stop(PSnake snake)
{
	while (1)
	{
		SetPos(10, 25);
		system("pause");
	}
}

void Move(PSnake snake)
{
	pNode pNext = NULL;
	pNext = (pNode)malloc(sizeof(Node));
	int x, y;
	//计算下一个节点坐标
	switch (snake->_dir)
	{
	case UP:
		x = snake->_pSnake->_x;
		y = snake->_pSnake->_y - 1;
		break;
	case DOWN:
		x = snake->_pSnake->_x;
		y = snake->_pSnake->_y + 1;
		break;
	case LEFT:
		x = snake->_pSnake->_x - 2;
		y = snake->_pSnake->_y;
		break;
	case RIGHT:
		x = snake->_pSnake->_x + 2;
		y = snake->_pSnake->_y;
		break;
	}
	//判断是否为墙
	if (x == 0 || x == 56 || y == 0 || y == 26)
	{
		snake->_status = BUMP_BY_WALL;
		return;
	}
	//判断是否自杀
	pNode cur = snake->_pSnake;
	while (cur)
	{
		if (x == cur->_x && y == cur->_y)
		{
			snake->_status = BUMP_BY_SELF;
			return;
		}
		cur = cur->_next;
	}
	//查看是否是食物
	if (x == snake->_food->_x && y == snake->_food)//是食物
	{
		pNext->_x = x;
		pNext->_y = y;
		pNext->_next = snake->_pSnake;
		snake->_pSnake = pNext;
	}
	PrintSnake(snake);
	Sleep(snake->_SleepTime);
}

void PrintSnake(PSnake snake)
{

}
void GameRun(PSnake snake)
{
	while (snake->_status == NORMAL)
	{
		//打印当前分数状态
		PrintScoreInfo(snake);
		//获取VK键
		if (Key_Press(VK_UP) && snake->_dir != DOWN)//输入w且蛇的方向不为下
		{
			snake->_dir = UP;
		}
		else if (Key_Press(VK_DOWN) && snake->_dir != UP)
		{
			snake->_dir = DOWN;
		}
		else if (Key_Press(VK_LEFT) && snake->_dir != RIGHT)
		{
			snake->_dir = LEFT;
		}
		else if (Key_Press(VK_RIGHT) && snake->_dir != LEFT)
		{
			snake->_dir = RIGHT;
		}
		else if (Key_Press(VK_ESCAPE))
		{
			snake->_status = END_NORMAL;
			break;
		}
		else if (Key_Press(VK_SPACE))
		{
			Stop(snake);
		}
		else if (Key_Pree(VK_F3))
		{
			//蛇的速度上限为100
			if (snake->_SleepTime > 100)
			{
				snake->_SleepTime -= 20;
				snake->_Add += 1;
			}
		}
		else if (Key_Pree(VK_F4))
		{
			//蛇的速度下限为300
			if (snake->_SleepTime < 300)
			{
				snake->_SleepTime += 20;
				snake->_Add -= 1;
			}
		}
		Move(snake);
	}
}

void GameEnd(PSnake snake)
{

}
